package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.exception.OrderBusinessException;
import com.sky.mapper.AdminOrderMapper;
import com.sky.mapper.OrderDetailMapper;
import com.sky.mapper.OrderMapper;
import com.sky.result.PageResult;
import com.sky.service.AdminOrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Title: AdminOrderServiceImpl
 * @Author 贾金博
 * @Package com.sky.service.impl
 * @Date 2024/3/30 20:37
 * @description:
 */

@Service
public class AdminOrderServiceImpl implements AdminOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private AdminOrderMapper adminOrderMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    /**
     * 订单查询
     * @param ordersPageQueryDTO
     * @return
     */
    @Override
    public PageResult conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
        PageHelper.startPage(ordersPageQueryDTO.getPage(), ordersPageQueryDTO.getPageSize());
        Page<Orders> orders = adminOrderMapper.findAllOrders(ordersPageQueryDTO);
        Page<OrderVO>orders2 = new Page<>();

        for(Orders order : orders) {
            List<OrderDetail> orderDetails = orderDetailMapper.getOrderDetailByOrderId(order.getId());
            String s = "";
            for(OrderDetail orderDetail : orderDetails) {
                s = s + orderDetail.getName() + " * " + orderDetail.getNumber() + " ; ";
            }
            OrderVO t = new OrderVO();
            BeanUtils.copyProperties(order, t);
            t.setOrderDishes(s);
            orders2.add(t);
        }
        PageResult pageResult = new PageResult(orders2.getTotal(), orders2.getResult());
        return pageResult;
    }

    /**
     * 各个状态的订单数量统计
     * @return
     */
    @Override
    public OrderStatisticsVO statistics() {
        //已接单
        Integer confirmed = adminOrderMapper.getOrdersByStatus(Orders.CONFIRMED);
        Integer toBeConfirmed = adminOrderMapper.getOrdersByStatus(Orders.TO_BE_CONFIRMED);
        Integer deliveryInProgress = adminOrderMapper.getOrdersByStatus(Orders.DELIVERY_IN_PROGRESS);

        OrderStatisticsVO orderStatisticsVO = new OrderStatisticsVO();

        orderStatisticsVO.setConfirmed(confirmed);
        orderStatisticsVO.setToBeConfirmed(toBeConfirmed);
        orderStatisticsVO.setDeliveryInProgress(deliveryInProgress);
        return orderStatisticsVO;
    }

    /**
     * 查询订单详情
     * @param id
     * @return
     */
    @Override
    public OrderVO findByOrderId(Long id) {
        Orders orders = orderMapper.getByOrdersId(id);
        OrderVO orderVO = new OrderVO();
        BeanUtils.copyProperties(orders, orderVO);
        List<OrderDetail> orderDetails = orderDetailMapper.getOrderDetailByOrderId(orders.getId());
        String s = "";
        for(OrderDetail orderDetail : orderDetails) {
            s = s + orderDetail.getName() + " * " + orderDetail.getNumber();
        }
        orderVO.setOrderDishes(s);
        orderVO.setOrderDetailList(orderDetails);
        return orderVO;
    }


    /**
     * 订单拒绝
     * @param ordersRejectionDTO
     */
    @Override
    public void rejection(OrdersRejectionDTO ordersRejectionDTO) {
        Orders orders = orderMapper.getByOrdersId(ordersRejectionDTO.getId());
        if(orders.getStatus() != 2) {
            throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
        }
        //拒绝订单
        orders.setStatus(Orders.CANCELLED);
//        orders.setCancelReason("商家取消了订单");
//        orders.setCancelTime(LocalDateTime.now());
        orders.setRejectionReason(ordersRejectionDTO.getRejectionReason());
        orderMapper.update(orders);
    }

    /**
     * 接单
     * @param ordersConfirmDTO
     */
    @Override
    public void confirm(OrdersConfirmDTO ordersConfirmDTO) {
        Orders orders = orderMapper.getByOrdersId(ordersConfirmDTO.getId());
        if(orders.getStatus() != 2) {
            throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
        }
        //接单
        orders.setStatus(Orders.CONFIRMED);
        orderMapper.update(orders);
    }

    /**
     * 派送订单
     * @param id
     * @return
     */
    @Override
    public void delivery(Long id) {
        Orders orders = orderMapper.getByOrdersId(id);
        if(orders.getStatus() != Orders.CONFIRMED) {
            throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
        }
        orders.setStatus(Orders.DELIVERY_IN_PROGRESS);
        orderMapper.update(orders);
    }

    /**
     * 完成订单
     * @param id
     */
    @Override
    public void complete(Long id) {
        Orders order = orderMapper.getByOrdersId(id);
        check(order, Orders.DELIVERY_IN_PROGRESS);
        order.setStatus(Orders.COMPLETED);
        order.setDeliveryTime(LocalDateTime.now());
        orderMapper.update(order);
    }

    /**
     * 取消订单
     * @param ordersCancelDTO
     */
    @Override
    public void cancel(OrdersCancelDTO ordersCancelDTO) {
        Orders orders = orderMapper.getByOrdersId(ordersCancelDTO.getId());
        orders.setCancelTime(LocalDateTime.now());
        orders.setCancelReason(ordersCancelDTO.getCancelReason());
        orders.setStatus(Orders.CANCELLED);
        orderMapper.update(orders);
    }

    private Boolean check(Orders orders, Integer status) {
        if(!orders.getStatus().equals(status)) {
            throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
        }
        return true;
    }
}
